package com.client;

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JDialog;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.JButton;

import com.metier.logique.MSG;
import com.metier.persistance.Carte;
import com.metier.persistance.Poi;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

/**
 * @name DialogUpdateLieu
 * @description Formulaire de modification d'un lieu.
 * @method DialogUpdateLieu - Constructeur par défaut.
 * @method DialogUpdateLieu(MSG) - Constructeur surchargé permettant.
 * @method buildDialog - Construit la JDialog.
 * @method checkBtnMettreJour - Active le bouton Valider si toutes les conditions sont remplies.
 * 
 * @author Ambroise
 */
public class DialogUpdateLieu extends JDialog implements ActionListener, KeyListener {
	
	private static final long serialVersionUID = 1L;
	private JTextField txtNom;
	private JTextArea txtDescription;
	private JButton btnMettreJour;
	private JLabel lblDescription;
	private JLabel lblNomDuLieu;
	private JButton btnAnnuler;
	private Carte carte;
	
	public DialogUpdateLieu(MSG oMSG) {
		buildDialog();
		
		// On sauvegarde la carte en local.
		this.carte = (Carte) oMSG.getData("carte");
		
		// On modifie les éléments de la fenêtre.
		this.txtNom.setText(this.carte.getNom());
		this.txtDescription.setText(this.carte.getDescriptif());
	}

	
	public void buildDialog(){
		setLocationRelativeTo(null);
		setSize(280, 300);
		setResizable(false);
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.addKeyListener(this);
		
		lblNomDuLieu = new JLabel("Nom du Lieu :");
		
		txtNom = new JTextField();
		lblNomDuLieu.setLabelFor(txtNom);
		txtNom.setColumns(10);
		this.txtNom.addKeyListener(this);
		
		lblDescription = new JLabel("Description : ");
		
		txtDescription = new JTextArea();
		lblDescription.setLabelFor(txtDescription);
		
		btnMettreJour = new JButton();
		btnMettreJour.setText("Modifier");
		btnMettreJour.addActionListener(this);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(this);
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNomDuLieu)
						.addComponent(txtNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblDescription)
						.addComponent(txtDescription, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnMettreJour)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnAnnuler)))
					.addContainerGap(25, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNomDuLieu)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblDescription)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtDescription, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnMettreJour)
						.addComponent(btnAnnuler))
					.addContainerGap())
		);
		getContentPane().setLayout(groupLayout);
	}
	
	/**
	 * @name checkBtnMettreJour
	 * @description Active le bouton Valider si toutes les conditions sont remplies.
	 */
	private void checkBtnMettreJour(){
		if(!this.txtNom.getText().isEmpty()){
			// Si les champs voulus sont remplis on active le bouton.
			this.btnMettreJour.setEnabled(true);
		}else{
			// Sinon on le désactive.
			this.btnMettreJour.setEnabled(false);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if (source == this.btnMettreJour) {
			MSG oMSG = new MSG();
			Carte carte = new Carte();
			SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); 
			Date date = new Date(); 
			
			// On crée un MSG contenant les informations à modifier.
			oMSG.setData("id", this.carte.getIdCarte());
			oMSG.setData("NOM", this.txtNom.getText());
			oMSG.setData("DESCRIPTIF", this.txtDescription.getText());
			oMSG.setData("DATE_MODIFICATION", formater.format(date));
			
			// On update la carte.
			oMSG = carte.update(oMSG);
			
			this.setVisible(false);
		}
		
	}


	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void keyPressed(KeyEvent e) {
		Object source = e.getSource();
		int keyCode = e.getKeyCode();
		
		if(source == this.txtNom){
			checkBtnMettreJour();
		}else if(source == this.txtNom){
			// Bouton Entrée -> On valide manuellement le bouton valider.
			if(keyCode == 10){
				this.btnMettreJour.doClick();
			}
		}
	}


	@Override
	public void keyReleased(KeyEvent e) {
		Object source = e.getSource();
		char keyChar = e.getKeyChar();
		int keyCode = e.getKeyCode();
		
		if(keyCode == 27){
			// Bouton echap
			this.setVisible(false);
		}
	}
}
